﻿#include "savethread.h"
#include "tool.h"

SaveThread::SaveThread(QObject *parent,fsConfig *conf, const QString curDir)
{
    retInfo("init SaveThread");
    // queueMutex = new QMutex();
    isExit = false;
    threadId = (int)currentThreadId();

    if(conf != nullptr){
        fsConf = conf;
    }
    curPath = curDir;
    curFileNum = 1;
}

void SaveThread::stop(void){
    isExit = true;
}

// SaveThread::~SaveThread()
// {
//     isExit = true;
//     delete queueMutex;
//     retInfo("release SaveThread");
// }

int SaveThread::getImageList(void){
    return saveImgList.count();
}

int SaveThread::getLogList(void){
    return saveLogList.count();
}

void SaveThread::run(){
    while(!isExit){
        QThread::msleep(100);
        while(saveLogList.count()){
            //存日志
            QString msg = saveLogList.takeFirst();
            saveLogName(msg,QString("img%0").arg(threadId));
        }
        if(saveImgList.count()){
            //存图片
            QString name="",ext="";
            ext = QString(fsConf->designVar.SaveFileExt).split(",").at(fsConf->designVar.SaveFormat);
            setFilename(curFileNum++,name,curPath,ext,fsConf->listVar.SaveFileLen);

            cv::Mat img = saveImgList.takeFirst();
            retInfo(QString("save img %0 %1 %2").arg(img.cols).arg(img.rows).arg(name));
            cvMatImageSave(img, name, false, fsConf->designVar.SaveFormat, fsConf->designVar.Para);
            saveImageEx(name, fsConf->designVar.SaveDPI, (1-fsConf->designVar.SaveHeight)*270);
            emit saveFile(name);
            // QThread::sleep(3);
        }
    }
    retInfo("exit SaveThread");
    // delete queueMutex;
}

void SaveThread::AddImg(const cv::Mat img)
{
    saveImgList.append(img);
}

void SaveThread::AddLog(const QString msg){
    saveLogList.append(msg);
}
